package com.qf.springmq.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.qf.springmq.mapper.VoteMapper;
import com.qf.vote2203.dbvote.entity.Vote;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @Author liujinchi
 * @Date 2023/3/2 11:17
 * @注释
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("/add")
    public String addUser(String phone) {

        Long size = redisTemplate.opsForList().size("vote" + phone);

        List<Vote> list = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            Vote o = (Vote) redisTemplate.opsForList().leftPop("vote" + phone);
         // JSON o1 = (JSON) JSON.toJSON(o);
            list.add(o);
            if (size<10&&i+1==size){
                rabbitTemplate.convertAndSend("vote", "yg", list);
            }
            if ((i+1)%10==0) {
                rabbitTemplate.convertAndSend("vote", "yg", list);
                list.removeAll(list);
           }
        }
        rabbitTemplate.convertAndSend("vote", "yg", list);

        return "ok";
    }


}
